package com.lenovo.android.syncacc.activities;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.lenovo.android.syncacc.MyAppInitializer;
import com.lenovo.android.syncacc.R;
import com.lenovo.android.syncacc.controller.MyAndroidController;

public class TestAccount extends Activity {
	Button bt_get;
	Button bt_add;
	Button bt_del;

	TextView tv_result;
	MyAndroidController controller;
	Account account;
	MyAppInitializer initializer;
	AccountManager am;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		controller = new MyAndroidController(this);
		initializer = new MyAppInitializer(this);
		am = (AccountManager) AccountManager.get(this);

		tv_result = (TextView) this.findViewById(R.id.tv_result);

		bt_get = (Button) this.findViewById(R.id.bt_get);
		bt_add = (Button) this.findViewById(R.id.bt_add);
		bt_del = (Button) this.findViewById(R.id.bt_del);

		bt_del.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				account = controller.getNativeAccount();
				if (account != null) {
					am.removeAccount(account, null, null);
					tv_result.setText("delete the account");
				} else {
					tv_result.setText("no account to delete");
				}
			}
		});

		bt_get.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				account = controller.getNativeAccount();
				if (account == null) {
					tv_result.setText(R.string.account_null);
				} else {
					tv_result.setText(account.toString());
				}
			}
		});

		bt_add.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				account = controller.getNativeAccount();
				if (account == null) {
					initializer.initAccount(TestAccount.this);
				} else {
					tv_result.setText("Account already exists");
				}

			}
		});
	}
}